package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.RebateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 邀请商家返利
 */
@Controller
@RequestMapping(ControllerConstants.REBATE)
public class RebateMCtronller extends BaseMobileController {
    @Autowired
    private RebateService rebateService;

    /**
     * 获取分享注册商家的商家数量，及订单数量
     */
    @RequestMapping("/rebateCount")
    public void rebateCount(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                return rebateService.rebateCount(userId);
            }
        });
    }

    /**
     * 返利 红包数量、金额、列表
     */
    @RequestMapping("/findRebateList")
    public void findRebateList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                Integer nowPage = getInteger(json, "nowPage", 1);
                Integer pageSize = getInteger(json, "pageSize", 20);
                return rebateService.findRebateList(userId, nowPage, pageSize);
            }
        });
    }

    /**
     *  红包爆发点击事件，取消点击的红包
     */
    @RequestMapping("/cancelRebate")
    public void cancelRebate(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer rebateId = getInteger(json, "rebateId", 0);
                Integer userId = getInteger(json, "userId", 0);

                return rebateService.cancelRebate(rebateId,userId);
            }
        });
    }
}
